package nio;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @ClassName NioCopyDemo
 * @Description TODO
 * @Author leejiliang
 * @DateTime 2021/8/26 13:40
 * @Version 1.0
 */
public class NioCopyDemo {
    public static void main(String[] args) throws IOException {
        nioCopy();
    }

    public static void nioCopy() throws IOException {

        var file = new File("/Users/leejiliang/Downloads/工位导入.xlsx");
        var file2 = new File("/Users/leejiliang/Downloads/工位导入3.xlsx");
        var inputStream = new FileInputStream(file);
        var outputStream = new FileOutputStream(file2);
        var outChannel = outputStream.getChannel();
        var inputChannel = inputStream.getChannel();
        var byteBuffer = ByteBuffer.allocate(1024);

        if (!file2.exists()) {
            file2.createNewFile();
        }

        while (inputChannel.read(byteBuffer) != -1) {
            System.out.println("读");
            byteBuffer.flip();
            while (outChannel.write(byteBuffer) != 0) {
                System.out.println("写");
            }
            byteBuffer.clear();
        }

        outChannel.force(true);

        inputChannel.close();
        outChannel.close();
        inputStream.close();
        outputStream.close();
    }
}
